package com.opc.work;

import com.opc.config.OpcClientConfigBean;
import com.opc.service.DeviceService;
import com.opc.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;

/**
 * @author SEG
 */
@Slf4j
@Component
public class OpcStatusCheckSchedule2 {
    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private OpcClientConfigBean clientConfigBean;

    @Autowired
    private DeviceService deviceService;

    /**
     * 检测设备状态的接口，每3小时检测一次
     */
//    @Scheduled(cron = "0/10 * * * * ?")
//    @Scheduled(cron = "0 0 0/3 * * ?")
    @Scheduled(cron = "0 0/10 * * * ?")
    public void consumeReading() throws IOException {
        List<String> line = Arrays.asList("PVS1", "PVS6", "ROG7", "ROG8");
        List<String> list = Arrays.asList("192.168.1.10", "192.168.1.20", "192.168.1.30", "192.168.1.40");
        for (int i = 0; i < list.size(); i++) {
            InetAddress geek = InetAddress.getByName(list.get(i));
            System.out.println("Sending Ping Request to " + list.get(i));
            if (geek.isReachable(5000)) {
                System.out.println("Host is reachable");
                redisUtils.set(line.get(i), 1);
            } else {
                System.out.println("Sorry ! We can't reach to this host");
                redisUtils.set(line.get(i), 0);
            }
        }
    }

}



